//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Scene2D/Types/SceneObjMsg.h"
#include "Scene2D/Render/RenderObject.h"
#include "Scene2D/SceneManager.h"

namespace Engine
{
namespace Scene2D
{

	struct ESceneObj
	{
		enum type
		{
			BASE,
			CAMERA,
			TRIGGER,
		};
	};



	//
	// Scene Object
	//

	class SceneObject
	{
	// variables
	protected:
		Ptr< SceneManager >		_sm;
		Transform2D				_transform;
		ESceneObj::type const	_objType;


	// methods
	public:
		SceneObject (ESceneObj::type type) : _objType(type)	{}
		
		virtual ~SceneObject ()	{}


		Transform2D const &	Transform ()		const	{ return _transform; }
		ESceneObj::type		GetType ()			const	{ return _objType; }

		Ptr<SceneManager>	GetSceneManager ()	const	{ return _sm; }


	// interface
	public:
		virtual bool ProcessMessage (const SceneObjMsg &msg);


	// hidden methods
	protected:
		bool _Attach (const SceneObjMsg &msg);
		bool _Detach (const SceneObjMsg &msg);
	};

	

/*
=================================================
	ProcessMessage
=================================================
*/
	inline bool SceneObject::ProcessMessage (const SceneObjMsg &msg)
	{
		typedef SceneObjMsg		SO;

		switch ( msg.GetType() )
		{
			case SO::ATTACH_TO_SCENE :		return _Attach( msg );
			case SO::DETACH_FROM_SCENE :	return _Detach( msg );
		}
		return false;
	}
	
/*
=================================================
	_Attach
=================================================
*/
	inline bool SceneObject::_Attach (const SceneObjMsg &msg)
	{
		typedef SceneObjMsg		SO;

		SO::AttachToScene const &	m = msg.To< SO::AttachToScene >();

		CHECK_ERR( m.obj == this );

		_sm = m.sm;
		return true;
	}
	
/*
=================================================
	_Detach
=================================================
*/
	inline bool SceneObject::_Detach (const SceneObjMsg &msg)
	{
		typedef SceneObjMsg		SO;

		SO::AttachToScene const &	m = msg.To< SO::AttachToScene >();

		CHECK_ERR( m.obj == this );

		_sm = null;
		return true;
	}

	
}	// Scene2D
}	// Engine